package com.sgb.kjwl.viewmodel.packers;

import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.FindWorkerSearchEntity;
import com.sgb.link.entity.PickFilterEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.WorkYear;
import com.sgb.kjwl.model.entity.WorkerLevel;
import com.sgb.kjwl.view.ui.fragment.packers.FindWorkerSearchFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FragFindWorkerSearchModel extends BaseViewModel {

    private FindWorkerSearchFragment fragment;

    private PickSelectEntity all;

    private int page = 1;
    private int limit = 10;

    public FragFindWorkerSearchModel(FindWorkerSearchFragment fragment) {
        this.fragment = fragment;
        all = new PickSelectEntity();
        all.setNo("");
        all.setProjectscale("全部");
    }

    public void getFindWorkerSearch(String keyword, String industryNo, String categoryNo, String workType, String workLevel, String workYear, String provinceNo, String cityNo, String areaNo, boolean b) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map<String,Object> map = new HashMap<>();
        page = b ? page + 1 : 1;
        map.put("limit", limit);
        map.put("page", page);
        map.put("keyword", keyword);
        if (!TextUtils.isEmpty(industryNo)) {
            map.put("industryNo", industryNo);
        }
        if (!TextUtils.isEmpty(categoryNo)) {
            map.put("categoryNo", categoryNo);
        }
        if (!TextUtils.isEmpty(workType)) {
            map.put("workType", workType);
        }
        if (!TextUtils.isEmpty(workLevel)) {
            map.put("proficiency", workLevel);
        }
        if (TextUtils.isEmpty(workYear)) {
            map.put("workYear", "0");
        } else {
            map.put("workYear", workYear);
        }
        if (!TextUtils.isEmpty(provinceNo)) {
            map.put("provinceNo", provinceNo);
        }
        if (!TextUtils.isEmpty(cityNo)) {
            map.put("cityNo", cityNo);
        }
        if (!TextUtils.isEmpty(areaNo)) {
            List<String> stringList = new ArrayList<>();
            stringList.add(areaNo);
            map.put("areaNo", stringList);
        }
        APIManager.getInstance().getPackerAPI().getFindWorkerSearch(map).enqueue(new Callback<BaseEntity<List<FindWorkerSearchEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<FindWorkerSearchEntity>>> call, Response<BaseEntity<List<FindWorkerSearchEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showFindWorkerList(response.body().getData(), b, response.body().getData() == null || response.body().getData().size() < limit);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<FindWorkerSearchEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    // -------------------------------- 服务团队-联动工种 --------------------------------

    public void getLinkAgeWorkType() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getLinkAgeWorkType1().
                enqueue(new Callback<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>> call, Response<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null || response.body().getState() == null) {
                            return;
                        }
                        if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            fragment.showWorkTypePop(response.body().getData());
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    // -------------------------------- 工龄 --------------------------------

    public void getWorkYear() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getWorkYearList().enqueue(new Callback<BaseEntity<List<WorkYear>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<WorkYear>>> call, Response<BaseEntity<List<WorkYear>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null && response.body().getState() != null && response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<WorkYear> workYears = response.body().getData();
                    List<PickSelectEntity> data = new ArrayList<>();
                    if (workYears != null && workYears.size() > 0) {
                        for (WorkYear workYear : workYears) {
                            PickSelectEntity entity = new PickSelectEntity();
                            entity.setNo(String.valueOf(workYear.getType()));
                            entity.setProjectscale(workYear.getDesc());
                            data.add(entity);
                        }
                    }
                    fragment.shoWorkYearPop(data);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<WorkYear>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    // -------------------------------- 工人级别 --------------------------------

    public void getWorkerLevel() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getWorkerLevel().
                enqueue(new Callback<BaseEntity<List<WorkerLevel>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<List<WorkerLevel>>> call, Response<BaseEntity<List<WorkerLevel>>> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null || response.body().getState() == null) {
                            return;
                        }

                        if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            List<WorkerLevel> workYears = response.body().getData();
                            List<PickSelectEntity> data = new ArrayList<>();
                            if (workYears != null && workYears.size() > 0) {
                                for (WorkerLevel workerLevel : workYears) {
                                    PickSelectEntity entity = new PickSelectEntity();
                                    entity.setNo(String.valueOf(workerLevel.getType()));
                                    entity.setProjectscale(workerLevel.getDesc());
                                    data.add(entity);
                                }
                            }
                            data.add(0, all);
                            fragment.shoWorkerLevelPop(data);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<List<WorkerLevel>>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    // -------------------------------- 省市区 --------------------------------

    public void getCityList() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getCityList().enqueue(new Callback<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>> call, Response<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null && response.body().getData().size() > 0) {
                        for (int i = 0; i < response.body().getData().size(); i++) {
                            fragment.showCityPop(response.body().getData());
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
